package com.health.mapp.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;

import com.health.commons.PropertiesService;
import com.health.db.dto.StuffDetailDto;
import com.health.db.model.IMsgIMMessage;
import com.health.db.model.IMsgIMMessageExt;
import com.health.db.model.RegisterOrder;
import com.health.db.model.RegisterOrderExample;
import com.health.db.model.StuffListTemp;
import com.health.db.model.UserLISTTemp;
import com.health.db.service.msg.IIMsgIMMessageService;
import com.health.db.service.order.IRegisterOrderService;
import com.health.db.service.user.IStuffListService;
import com.health.mapp.bean.GetCommHistoryReq;
import com.health.mapp.bean.GetCommHistoryResp;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.dto.IMessageDto;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * Created by helili on 2017/3/21.
 */
public class GetCommHistoryImpl implements ActionInterface {

    private Logger logger = Logger.getLogger(GetCommHistoryImpl.class);

    private IIMsgIMMessageService iMsgIMMessageService;
    
    private IRegisterOrderService registerOrderService  ;
    
    private IStuffListService stuffListService  ;
     
    @Override
    public Response execute(Header header, Object reqBean) throws Exception {
        GetCommHistoryReq req = (GetCommHistoryReq) reqBean;
        GetCommHistoryResp resp = new GetCommHistoryResp();
        Integer pageNo=req.getPageNo();
		Integer pageSize=req.getPageNum();
		resp.setPageNo(pageNo);
		resp.setPageNum(pageSize);
        Long orderId =  req.getOrderId();
        //参数校验
        if(orderId == null){
            resp.setErrorMsg("会话id不能为空");
            resp.setErrorCode(-1);
            return resp;
        }
        IMsgIMMessageExt query=new IMsgIMMessageExt();
        query.setGroupid(orderId);
        //设置分页查询
        if(null!=pageNo&&null!=pageSize){
        	query.setOffset((pageNo.intValue()-1)*pageSize.intValue());
        	query.setPageSize(pageSize.intValue());
		}
        
        List<IMsgIMMessage> messageList = iMsgIMMessageService.selectByCondition(query);
        Collections.reverse(messageList);
        resp.setPageNo(req.getPageNo());
        resp.setPageNum(req.getPageNum());
        if (messageList != null && messageList.size() > 0) {
        	
        	RegisterOrderExample temp = new RegisterOrderExample();
        	temp.setRegisterid(req.getOrderId());
        	List<RegisterOrder> orderList =  registerOrderService.selectSimple(temp);
        	
        	if (null!=orderList&&orderList.size()==1) {
        		
        		   RegisterOrder  currRegisterOrder =orderList.get(0);
        		   UserLISTTemp sender = currRegisterOrder.getUser();
                 
                   //和现网保持一致
                   resp.setUserID(currRegisterOrder.getUserid().toString());
                  // resp.setUserName(sender.getUserName());
                  
                   //resp.setUserImageUrl(sender.getIcon());
            
                   resp.setUserName("匿名用户");
                   //user_common_image
                   resp.setUserImageUrl(PropertiesService.getString("user_common_image").trim());
                   
                   //医生头像另外查stuff表
                   StuffListTemp doctor  =currRegisterOrder.getDoctor();
                   if(doctor !=null){
                	   
                	   StuffDetailDto stuffDetailDto = stuffListService.selectByStuffId(Long.valueOf(doctor.getDocId()));
                       resp.setDocImageUrl(doctor.getDocImageUrl());
                       resp.setDocId(currRegisterOrder.getAccepterid().toString());
                       resp.setDocName(doctor.getDocName());
                       
                       resp.setHospitalName(stuffDetailDto.getHospitalName());
                       resp.setDepartmentName(stuffDetailDto.getDepartmentName());
                       resp.setOrderCount(stuffDetailDto.getOrderCount()==null?0:stuffDetailDto.getOrderCount());
                       resp.setDocTitle(stuffDetailDto.getStuffTitle());
                   }
			}else{
				resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
				resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
				return resp;
			}

            List<IMessageDto> historyList = new ArrayList<IMessageDto>();
            
            int size = messageList.size();
            for (int i = size-1; i >=0; i--) {
            	IMsgIMMessage message = messageList.get(i);
			//}
          //  for (IMsgIMMessage message : messageList) {
                IMessageDto messageDto = new IMessageDto();
                messageDto.setUserId(message.getSenderid().toString());
                messageDto.setContent(message.getMessageText());
                messageDto.setDateTime(DateUtils.formatDatetime(message.getMessageTime()));
                messageDto.setType(message.getMediatype());
                messageDto.setUrl(message.getMediaUrl());
                historyList.add(messageDto);
            }
            resp.setHistoryList(historyList);
        }
         
        return resp;
    }

	public Logger getLogger() {
		return logger;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public IIMsgIMMessageService getiMsgIMMessageService() {
		return iMsgIMMessageService;
	}

	public void setiMsgIMMessageService(IIMsgIMMessageService iMsgIMMessageService) {
		this.iMsgIMMessageService = iMsgIMMessageService;
	}

	/**
	 * @return the registerOrderService
	 */
	public IRegisterOrderService getRegisterOrderService() {
		return registerOrderService;
	}

	/**
	 * @param registerOrderService the registerOrderService to set
	 */
	public void setRegisterOrderService(IRegisterOrderService registerOrderService) {
		this.registerOrderService = registerOrderService;
	}

	public IStuffListService getStuffListService() {
		return stuffListService;
	}

	public void setStuffListService(IStuffListService stuffListService) {
		this.stuffListService = stuffListService;
	}
        
  
}
